<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\9\18 0018
 * Time: 16:41
 */

namespace app\Lib\Model\HandleLog;


use app\Lib\Exception\BlException;
use app\Lib\InterfaceIn\HandleLog\HandleLogListHandle;
use app\Lib\Model\DbModel\HandleLogs;

class HandleLogList implements HandleLogListHandle
{
    private $keyword;
    private $keywordType;
    private $startTime;
    private $endTime;
    private $type;
    private $obj;
    private $table;
    private $page =1;
    private $limit=10;
        /**
         * 搜索内容
         * @param $keyword
         * @return $this
         */
    public function setKeyword($keyword)
    {
        $this->keyword = trim($keyword);
        return $this;
    }

    /**
     * 搜索类型
     * @param $keywordType
     * @return $this
     */
    public function setKeywordType($keywordType)
    {
        $this->keywordType = trim($keywordType);
        return $this;
    }

    /**
     * 搜索开始时间
     * @param $time
     * @return $this
     */
    public function setStartTime($time)
    {
        $this->startTime = intval($time);
        return $this;
    }

    /**
     * 搜索结束时间
     * @param $time
     * @return $this
     */
    public function setEndTime($time)
    {
        $this->endTime = intval($time);
        return $this;
    }

    /**
     * 操作类型
     * @param $type
     * @return $this
     */
    public function setType($type)
    {
        $this->type = trim($type);
        return $this;
    }

    /**
     * 操作对象
     * @param $obj
     * @return $this
     */
    public function setObj($obj)
    {
        $this->obj = trim($obj);
        return $this;
    }

    /**
     * 设置页数
     * @param int $page
     * @return $this
     */
    public function setPage($page = 1)
    {
        $this->page = intval($page);
        return $this;
    }

    /**
     * 设置每页行数
     * @param int $limit
     * @return $this
     */
    public function setLimit($limit = 10)
    {
        $this->limit = intval($limit);
        return $this;
    }

    /**
     * 执行
     * @return array
     * @throws BlException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function run()
    {
        $model  = $this->getSelectObj();
        $count  = $model->count('*');
        if($count<=0){
            return ['count'=>0,'list'=>[]];
        }
        /*TODO $filed需要建立用户表才可完成*/
        $filed = [];
        $model  = $this->getSelectObj();
        $list   = $model->limit(($this->page-1)*$this->limit,$this->limit)->field($filed)->select();
        return ['count'=>$count,'list'=>$list];

    }

    /**
     * 筛选条件组装
     * @return array
     * @throws BlException
     */
    private function getMap(){
        $map    = [];
        $model  = $this->getTable();
        if(($this->startTime>0) && ($this->endTime>0) && $this->endTime<$this->startTime){
            throw new BlException('开始时间不允许大于结束时间');
        }
        if(($this->startTime>0) && ($this->endTime>0) && $this->endTime>$this->startTime){
            $map['logs.'.$model->_time] =['between',[$this->startTime,$this->endTime]];
        }
        if(($this->startTime>0) && empty($this->endTime)){
            $map['logs.'.$model->_time] =['gt',$this->startTime];
        }
        if(($this->endTime>0) && empty($this->startTime)){
            $map['logs.'.$model->_time] =['LT',$this->endTime];
        }
        if(!empty($this->keywordType) && !empty($this->keyword)){
            switch ($this->keywordType){
                case 'content':
                    $map['logs.'.$model->_content] = $this->keyword ;
                    break;
                case 'account':

                    break;
            }
        }
        if(!empty($this->type)){
            $map['logs.'.$model->_type] = $this->type;
        }
        if(!empty($this->obj)){
            $map['logs.'.$model->_obj]  = $this->obj;
        }
        return $map;
    }

    /**
     * 获取查询类
     * @return HandleLogs
     * @throws BlException
     */
    private function getSelectObj(){
        $map    = $this->getMap();
        $table  = $this->getTable();
        /*TODO 关联用户表*/
        $join=[
            []
        ];
        $table->alias('logs')->where($map)->join($join);
        return $table;
    }

    /**
     *
     * @return HandleLogs
     */
    private function getTable(){
        if(empty($this->table)){
            $this->table = new HandleLogs();
        }
        return $this->table;
    }

    private function getUserTable(){

    }

}